package com.aaa.account.controller;

import com.aaa.account.entity.TbAccAccount;
import com.aaa.account.service.impl.TbAccAccountServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("account")
public class AccountController {

    @Resource
    TbAccAccountServiceImpl tbAccAccountServiceImpl;

    @RequestMapping("selectAll")
    public List<TbAccAccount> selectAll(){
        return tbAccAccountServiceImpl.listDesc();
    }

    /*根据用户查全部订单*/
    @RequestMapping("updatePicpath")
    public boolean updatePicpath(String defaultPic,String accountId){
        return tbAccAccountServiceImpl.updatePicpath(defaultPic,accountId);
    }

    @RequestMapping("saveOrUpdate")
    public boolean saveOrUpdate(TbAccAccount tbAccAccount){
        return tbAccAccountServiceImpl.saveOrUpdate(tbAccAccount);
    }

    /*
     * 注册
     * */
    @RequestMapping("registered")
    public boolean registered(String phone, String password){
        return tbAccAccountServiceImpl.registered(phone,password);
    }


    @RequestMapping("delete")
    public boolean delete(Integer tid){
        return tbAccAccountServiceImpl.removeById(tid);
    }

    //模糊查询
    @RequestMapping("listByPhone")
    public List<TbAccAccount> listByLname(String phone){
        return tbAccAccountServiceImpl.listByPhone(phone);
    }


    // 获取图片
    @RequestMapping({"/findProductPicByPath"})
    public void getFeedBackPicture(HttpServletResponse response, @RequestParam("name") String name) throws IOException {
        response.setContentType("image/jpeg;charset=utf-8");
        response.setHeader("Content-Disposition", "inline; filename=girls.png");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(Files.readAllBytes(Paths.get(System.getProperty("user.dir")+"\\service_account\\src\\main\\resources\\img\\").resolve(name)));
        outputStream.flush();
        outputStream.close();
    }
    // 上传图片
    @RequestMapping({"/insertProductPic"})
    public Object insertProductPic(@RequestParam("file") MultipartFile avatorFile) {
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败");
            return jsonObject;
        } else {
            String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
            String filePath = System.getProperty("user.dir")+"\\service_account\\src\\main\\resources\\img";
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdir();
            }

            File dest = new File(filePath + System.getProperty("file.separator") + fileName);
            String storePath = fileName;

            try {
                avatorFile.transferTo(dest);
                jsonObject.put("code", 1);
                jsonObject.put("msg", "上传成功");
                jsonObject.put("pic", storePath);
                return jsonObject;
            } catch (IOException var15) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "上传失败" + var15.getMessage());
                return jsonObject;
            }
        }
    }
    // 修改图片
    @RequestMapping({"/updateProductPic"})
    public Object updateProductPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("tid") String tid) {
        JSONObject jsonObject = new JSONObject();
        if (avatorFile.isEmpty()) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败");
            return jsonObject;
        } else {
            String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
            String filePath = System.getProperty("user.dir")+"\\service_account\\src\\main\\resources\\img";
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdir();
            }

            File dest = new File(filePath + System.getProperty("file.separator") + fileName);
            String storePath = fileName;

            try {
                avatorFile.transferTo(dest);
                TbAccAccount tbAccAccount = new TbAccAccount();
                tbAccAccount.setTid(tid);
                tbAccAccount.setPicPath(storePath);
                boolean flag = tbAccAccountServiceImpl.updateById(tbAccAccount);
                if (flag) {
                    jsonObject.put("code", 1);
                    jsonObject.put("msg", "上传成功");
                    jsonObject.put("pic", storePath);
                    return jsonObject;
                } else {
                    jsonObject.put("code", 0);
                    jsonObject.put("msg", "上传失败");
                    return jsonObject;
                }
            } catch (IOException var15) {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "上传失败" + var15.getMessage());
                return jsonObject;
            }
        }
    }
}
